/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('$YourCompany$');
    
$YourCompany$.Edit$safeprojectname$ = function()
{
    //Call Base Method
    $YourCompany$.Edit$safeprojectname$.initializeBase(this);
    //Member Variables
    this._controls = [];
    this._msgs = {};
    this._settings = {};

    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        _updateSuccessDelegate: Function.createDelegate(this, this._updateSuccess),
        _updateFailDelegate: Function.createDelegate(this, this._updateFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad)
        };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

$YourCompany$.Edit$safeprojectname$.prototype =
{
    //Properties
    get_ns: function() {return this.get_id() + '_';},
    get_msgs: function() {return this._msgs;},
    set_msgs: function(value) {this._msgs = value;},
    get_settings: function() {return this._settings;},
    set_settings: function(value) {this._settings = value;},

    //Events
    initialize: function() 
    {
        //Call Base Method    
        $YourCompany$.Edit$safeprojectname$.callBaseMethod(this, 'initialize');
        
        //hookup event handlers
        $addHandlers(this._getControl('cmdUpdate'), { "click": this._onUpdate }, this);
    },

    _onLoad: function(src, args)
    {
        this._updateControls();
    },

    _onUpdate: function(src, arg)
    {   
        this._displayWait(true); 
        this._updateSettings();
        dnn.xmlhttp.callControlMethod('$YourCompany$.Modules.$safeprojectname$.Edit$safeprojectname$.' + this.get_id(), 
            'UpdateSettings', {Settings:this._settings}, this._delegates._updateSuccessDelegate, this._delegates._updateFailDelegate);
    },

    //Methods
    getMessage: function(key)
    {
        return this._msgs[key];
    },

    getSetting: function(key, def)
    {
        if (this._settings[key])
            return this._settings[key];
        return def;
    },

    //Private Methods
    _updateControls: function()
    {
        this._getControl('txtColor').value = this.getSetting('Color', '');
    },

    _updateSettings: function()
    {
        this._settings['Color'] = this._getControl('txtColor').value;
    },

    _displayWait: function(show)
    {
        this._getControl('imgAjax').className = (show ? '' : 'ceHidden');
    },

    _updateSuccess: function(payload, ctx, req)
    {
        this._displayWait(false);
        alert(this.getMessage('SettingsSaved'));
    },

    _updateFail: function(payload, ctx, req)
    {
        this._displayWait(false);
        alert('error: ' + payload);
    },

    _getControl: function(id)
    {
        if (this._controls[id] == null)
        {
            this._controls[id] = $get(id);
            if (this._controls[id] == null)
                this._controls[id] = $get(this.get_ns() + id);
        }
        return this._controls[id];
    },
    
    dispose: function()
    {
        for (var id in this._controls)
        {
            if (this._controls[id].tagName != null)
                $clearHandlers(this._controls[id]);
        }
        this._delegates = null;
        $YourCompany$.Edit$safeprojectname$.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
$YourCompany$.Edit$safeprojectname$.registerClass('$YourCompany$.Edit$safeprojectname$', Sys.Component);
